package org.jsola.hr.web;

import org.jsola.approval.vo.UserApproAllInfoVO;
import org.jsola.core.Result;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.provider.IApproProviderService;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.vo.EmpAndApproInfoVO;
import org.jsola.hr.vo.EmpInfoVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;


/**
 * @author zhr
 */
@RestController("hrApproProviderController")
@RequestMapping(HrConstants.API_PATH + "/v1/hrApproProviders")
public class HrApproProviderController implements IHrApproProviderControllerAPI {

    @Autowired
    private IApproProviderService approProviderService;
    @Autowired
    private IEmpInfoService empInfoService;

    @Override
    @GetMapping("/userApproDetails/{userApproId}")
    public Result<UserApproAllInfoVO> findUserApproDetailsById(
            @PathVariable(value = "userApproId") String userApproId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                approProviderService.findUserApproDetailsById(userApproId, tokenUser)
        );
    }

    @Override
    @GetMapping("/empApproDetails/{subject_type}/{subject_id}")
    public Result<EmpAndApproInfoVO> findEmpApproDetailsBySubject(
            @PathVariable(value = "subject_type") String subjectType,
            @PathVariable(value = "subject_id") String subjectId) {
        TokenUser tokenUser = UserContext.getUser();

        EmpInfoVO empInfoVO = empInfoService.selectById(subjectId, tokenUser.getSiteId());
        if (Objects.isNull(empInfoVO)) {
            return Result.fail("员工信息不存在或已删除");
        }
        return Result.success(
                approProviderService.findEmpApproDetailsBySubject(subjectType, subjectId, tokenUser)
        );

    }
}

